package com.shushan.exam.bean;

import com.android.log.LogUtil;
import com.android.net.bean.NetBaseBean;
import com.shushan.base.Constants;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: TBPaperBean
 * @Desciption: 同步练习试卷bean
 * @author: yichaohua
 * @date: 2018-04-18
 */
public class PaperBean extends NetBaseBean {

    private static final String TAG = "PaperBean";

    public int id = 0;
    public int video_id = 0;
    public int special_id = 0;
    public int outline_id = 0;

    private List<ExerciseBean> chineseExerciseList = new ArrayList<ExerciseBean>();
    private List<ExerciseBean> mathExerciseList = new ArrayList<ExerciseBean>();
    private List<ExerciseBean> englishExerciseList = new ArrayList<ExerciseBean>();

    @Override
    public void initByJson(JSONObject jsonObject) throws JSONException {
        LogUtil.i(TAG,"jsonObject=" + jsonObject.toString());

        JSONObject paperJson = jsonObject.optJSONObject("paper_info");
        if(paperJson != null){
            id = paperJson.optInt("id",0);
            video_id = paperJson.optInt("video_id",0);
            special_id = paperJson.optInt("special_id",0);
            outline_id = paperJson.optInt("outline_id",0);
        }

        JSONArray chineseArray = jsonObject.optJSONArray(Constants.SUBJECT_CHINESE_ID + "");
        if(chineseArray != null){
            for (int i = 0;i < chineseArray.length();i++){
                JSONObject json = chineseArray.optJSONObject(i);
                ExerciseBean exerciseBean = new ExerciseBean();
                exerciseBean.initByJson(json);
                chineseExerciseList.add(exerciseBean);
            }
        }

        JSONArray mathArray = jsonObject.optJSONArray(Constants.SUBJECT_MACH_ID + "");
        if(mathArray != null){
            for (int i = 0;i < mathArray.length();i++){
                JSONObject json = mathArray.optJSONObject(i);
                ExerciseBean exerciseBean = new ExerciseBean();
                exerciseBean.initByJson(json);
                mathExerciseList.add(exerciseBean);
            }
        }

        JSONArray englishArray = jsonObject.optJSONArray(Constants.SUBJECT_ENGLISH_ID + "");
        if(englishArray != null){
            for (int i = 0;i < englishArray.length();i++){
                JSONObject json = englishArray.optJSONObject(i);
                ExerciseBean exerciseBean = new ExerciseBean();
                exerciseBean.initByJson(json);
                englishExerciseList.add(exerciseBean);
            }
        }
    }

    public List<ExerciseBean> getChineseExerciseList() {
        return chineseExerciseList;
    }

    public void setChineseExerciseList(List<ExerciseBean> chineseExerciseList) {
        this.chineseExerciseList = chineseExerciseList;
    }

    public List<ExerciseBean> getMathExerciseList() {
        return mathExerciseList;
    }

    public void setMathExerciseList(List<ExerciseBean> mathExerciseList) {
        this.mathExerciseList = mathExerciseList;
    }

    public List<ExerciseBean> getEnglishExerciseList() {
        return englishExerciseList;
    }

    public void setEnglishExerciseList(List<ExerciseBean> englishExerciseList) {
        this.englishExerciseList = englishExerciseList;
    }

    public int getLength(){
        return chineseExerciseList.size()
                + mathExerciseList.size()
                + englishExerciseList.size();
    }

    public ExerciseBean get(int index){
        int size1 = chineseExerciseList.size();
        int size2 = mathExerciseList.size();

        ExerciseBean exerciseBean = null;
        if(index < size1){
            exerciseBean = chineseExerciseList.get(index);
        }else if(index < size1 + size2){
            exerciseBean = mathExerciseList.get(index - size1);
        }else {
            exerciseBean = englishExerciseList.get(index - (size1+size2));
        }

        return exerciseBean;
    }

    public void chineseAdd(ExerciseBean exerciseBean){
        if(exerciseBean != null){
            chineseExerciseList.add(exerciseBean);
        }
    }

    public void mathAdd(ExerciseBean exerciseBean){
        if(exerciseBean != null){
            mathExerciseList.add(exerciseBean);
        }
    }

    public void englishAdd(ExerciseBean exerciseBean){
        if(exerciseBean != null){
            englishExerciseList.add(exerciseBean);
        }
    }
}
